<?php
//error_reporting(0);
$cookieVerify = dirname(__FILE__)."/verify.tmp";
$cookieSuccess = dirname(__FILE__)."/1769.tmp";
if(!$_POST){
    // 获取cookie并保存
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://mem1.kaewgh508.btnrc.com:88/");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieVerify);
    $rs = curl_exec($ch);
    curl_close($ch);

    // 带上cookie抓取验证码，必须带上cookie，否则验证码不对应
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://mem1.kaewgh508.btnrc.com:88/user/ValidateImage.aspx");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieVerify);
    $rs = curl_exec($ch);
    // 把验证码在本地生成，二次拉取验证码可能无法通过验证
    @file_put_contents("verify.jpg",$rs);
    curl_close($ch);

    $imageData = urlencode(base64_encode(file_get_contents('http://bj.games.com:666/verify.jpg')));

    $code = curl($imageData);
    // 登录
    $ch = curl_init();
    // 用户名\密码
    $user = "liu0033";
    $pass = "As147147";
    $verify = $code['result']['code'];
    $url = "http://mem1.kaewgh508.btnrc.com:88/user/login_validate.aspx";

    // 返回结果存放在变量中，不输出
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieVerify);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
    curl_setopt($ch, CURLOPT_POST, true);
    $fields_post = array("loginName"=> $user, "loginPwd"=> $pass,"ValidateCode"=>$verify);
    $headers_login = array("User-Agent" => "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
    $fields_string = "";
    foreach($fields_post as $key => $value){
        $fields_string .= $key . "=" . $value . "&";
    }
    $fields_string = rtrim($fields_string , "&");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieSuccess);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $result= curl_exec($ch);
    curl_close($ch);
    var_dump($result);exit;
}

function curl($fileName = '') {

    $host = "http://jisuyzmsb.market.alicloudapi.com";
    $path = "/captcha/recognize";
    $method = "POST";
    $appcode = "39a08dff2ca54731866373e02b6ef4d6";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    //根据API的要求，定义相对应的Content-Type
    array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
    $querys = "type=n4";
    $bodys = "pic=".$fileName;
    $url = $host . $path . "?" . $querys;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
    $result = curl_exec($curl);
    $result =  '{'.preg_replace("#^.*?{([^}]+)}.*?$#us", "$1", $result).'}}';
    $result = json_decode($result,true);
    return $result;
}

